package sr.codemg.service.impl;

import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sr.codemg.config.JwtConfig;
import sr.codemg.config.RedisUtil;
import sr.codemg.entity.SysMenus;
import sr.codemg.mapper.SysMenusMapper;
import sr.codemg.mapper.SysRoleMenuMapper;
import sr.codemg.service.SysMenusService;
import sr.codemg.utils.Constants;
import sr.codemg.utils.ResponseCode;
import sr.codemg.utils.StaticFunc;
import sr.codemg.vm.Base;
import sr.codemg.vm.Menus;
import sr.codemg.vm.Selects;
import sr.codemg.vm.Tables;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 **/
@Service
public class SysMenusServiceImpl implements SysMenusService {

    @Autowired
    SysMenusMapper sysMenusMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public SysMenus get(String id) {
        return sysMenusMapper.get(id);
    }

    @Override
    public Tables findList(Map<String, Object> params) {
        StaticFunc.page(params);
        List<SysMenus> data = sysMenusMapper.findList(params);
        Integer count = sysMenusMapper.count(params);
        return new Tables(data, count);
    }

    @Override
    public Base.Response add(SysMenus entity) {
        sysMenusMapper.add(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response update(SysMenus entity) {
        sysMenusMapper.update(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Base.Response delete(String id) {
        sysMenusMapper.delete(id);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Selects.Response select(Selects.Request req) {
        req.configPage();
        List<Selects.Select> data = sysMenusMapper.select(req);
        Selects.Response res = new Selects.Response();
        res.setData(data);
        return res;
    }

    @Override
    public List<Menus> home(Claims thinning) {
        String userId = JwtConfig.getUserId(thinning);
        String roleId = JwtConfig.getRoleId(thinning);

        List<Menus> list = (List<Menus>) redisUtil.hget(userId, Constants.USER_CONFIG.ROLE_MENU);
        if (list != null && list.size() != 0) {
            return list;
        }
        String[] ids = roleId.split(",");
        List<SysMenus> menus = sysMenusMapper.findRoles(ids);

        Collections.sort(menus);
        list = new ArrayList<>();
        for (SysMenus m : menus) {
            if (!StringUtils.isEmpty(m.getSuperId())) continue;
            Menus item = new Menus(m.getTitle(), m.getKey(), m.getIcon(), m.getId());
            item.setChildren(readChild(menus, m));
            list.add(item);
        }
        redisUtil.hset(userId, Constants.USER_CONFIG.ROLE_MENU, list);
        redisUtil.expire(userId, Constants.USER_CONFIG.CACHE_TIMEOUT);
        return list;
    }

    @Override
    public List<Menus> roles(String userId) {
        String key = SysMenusServiceImpl.class.getName() + userId;
        List<Menus> list = (List<Menus>) redisUtil.get(key);
        if (list != null && list.size() != 0) {
            redisUtil.expire(key, Constants.SYS_TIMEOUT);
            return list;
        }
        List<SysMenus> menus = sysMenusMapper.getAll();
        list = new ArrayList<>();
        for (SysMenus m : menus) {
            if (!StringUtils.isEmpty(m.getSuperId())) continue;

            Menus item = new Menus(m.getTitle(), m.getKey(), m.getIcon(), m.getId());
            item.setChildren(readChild(menus, m));
            list.add(item);
        }
        redisUtil.set(key, list);
        redisUtil.expire(key, Constants.SYS_TIMEOUT);
        return list;
    }

    private List<Menus> readChild(List<SysMenus> data, SysMenus s) {
        List<Menus> list = new ArrayList<>();
        for (SysMenus m : data) {
            if (m.getSuperId() != null && m.getSuperId().equals(s.getId())) {
                Menus item = new Menus(m.getTitle(), m.getKey(), m.getIcon(), m.getId());
                item.setChildren(readChild(data, m));

                list.add(item);
            }
        }
        return list;
    }
}

